home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / system / ringd-0.000 / ringd-0 / ringd / ringd.c.debug < prev    next >
Encoding:
Text File  |  1995-02-02  |  5.0 KB  |  266 lines

  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <syslog.h>
  8. #include <termios.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <sys/file.h>
  12. #include <sys/stat.h>
  13. #include <sys/time.h>
  14. #include <sys/wait.h>
  15. #include "config.h"
  16.  
  17. int tty_open(void);
  18. int modem_reset(void);
  19. int get_rings(int);
  20. void shut_down(int);
  21.  
  22. int fd_modem, rings=0;
  23. char buf[32];
  24. time_t timer1;
  25.  
  26. int main(void)
  27. {
  28.         int got_rings;
  29.         pid_t pid;
  30.  
  31.     if ((pid = fork()) < 0)
  32.     {
  33.         perror("Couldn't fork() daemon process");
  34.         return(-1);
  35.     }
  36.     else
  37.         if (pid != 0)
  38.             exit(0);
  39.  
  40.     /* From here on out, it's daemon all the way... */
  41.  
  42.     setsid();
  43.     chdir("/");
  44.     umask(0);
  45.     close(STDIN_FILENO);
  46.     close(STDOUT_FILENO);
  47.     close(STDERR_FILENO);
  48.  
  49.     if (signal(SIGTERM, shut_down) == SIG_ERR)
  50.     {
  51.         syslog(LOG_ERR,strerror(errno));
  52.         shut_down(-1);
  53.     }
  54.  
  55.     openlog("ringd", LOG_PID, LOG_DAEMON);
  56.     syslog(LOG_NOTICE, "ringd 0.8 monitoring modem");
  57.  
  58.     while (1)
  59.     {
  60.         got_rings = 0;
  61.  
  62.         while (tty_open() < 0)
  63.         {
  64.             close (fd_modem);
  65.             syslog(LOG_NOTICE,"modem busy, waiting 5 mins...");
  66.             sleep (RECYCLE_TIME);
  67.         }
  68.  
  69.         syslog(LOG_NOTICE,"modem not busy, waiting for call");
  70.  
  71.         if (modem_reset() < 0)
  72.             shut_down(-1);
  73.  
  74.         while (!got_rings)
  75.         {
  76.             timer1 = 0;
  77.             time(&timer1);
  78.  
  79.             if (get_rings(3) < 0)
  80.             {
  81.                 rings=1;
  82.                 continue;
  83.             }
  84.             else
  85.                 rings=0;
  86.  
  87.             syslog(LOG_NOTICE,"got 3 rings");
  88.  
  89.             if (get_rings(2) < 0)
  90.             {
  91.                 rings=1;
  92.                 continue;
  93.             }
  94.             else
  95.                 rings=0;
  96.  
  97.             syslog(LOG_NOTICE,"got 2 rings");
  98.  
  99.             got_rings=1;
  100.         }
  101.  
  102.         if (close(fd_modem) < 0)
  103.         {
  104.             syslog(LOG_ERR,strerror(errno));
  105.             shut_down(-1);
  106.         }
  107.  
  108.         sleep(2);
  109.  
  110.         if ((pid=fork()) < 0)    /* Be a man and fork, he says... */
  111.         {            /* What utter crap and pain. */
  112.             syslog(LOG_ERR,strerror(errno));
  113.             shut_down(-1);
  114.         }
  115.         else
  116.             if (pid == 0)    
  117.                     execl(PROGRAM,PROGRAM,ARG,NULL);
  118.             else
  119.                 wait(NULL);
  120.  
  121.         sleep(2);
  122.         syslog(LOG_NOTICE,"dip spawned, recycling..");
  123.     }
  124. }
  125.  
  126.  
  127. int tty_open(void) /* Opens the modem, and sets terminal attributes. */
  128. {
  129.     struct termios term;
  130.  
  131.     if ((fd_modem = open("/dev/modem", O_RDWR | O_NOCTTY)) < 0)
  132.     {
  133.         syslog(LOG_ERR,strerror(errno));
  134.         shut_down(-1);
  135.     }
  136.  
  137.     if (isatty(fd_modem) == 0)
  138.     {
  139.         syslog(LOG_ERR,strerror(errno));
  140.         shut_down(-1);
  141.     }
  142.  
  143.     if (flock(fd_modem,LOCK_EX) < 0)
  144.     {
  145.         syslog(LOG_ERR,strerror(errno));
  146.         shut_down(-1);
  147.     }
  148.  
  149.     /* This right here is such a hack, even I have a hard time believing
  150.      * it.  I stole these values from kermit and stty -a.  I tried a
  151.      * number of other combinations, which didn't work, some help here
  152.      * would be greatly appreciated. */
  153.  
  154.     term.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
  155.     term.c_iflag = IGNBRK | IGNPAR;
  156.     term.c_lflag = 0;
  157.     term.c_oflag = 0;
  158.  
  159.     if (cfsetispeed(&term,BPS_RATE) < 0)
  160.         return(-1);
  161.     if (cfsetospeed(&term,BPS_RATE) < 0)
  162.         return(-1);
  163.  
  164.     if (tcsetattr(fd_modem, TCSANOW, &term) < 0)
  165.         return(-1);
  166.  
  167.     return(0);
  168. }
  169.  
  170. int modem_reset(void) /* Resets the modem, and checks for additional rings. */
  171. {
  172.         char command[]=RESET_STR;         /* Modem reset. */
  173.     int number;
  174.  
  175.         if (write(fd_modem, &command, 4) < 0)
  176.     {
  177.                 syslog(LOG_ERR,strerror(errno));
  178.                 shut_down(-1);
  179.     }
  180.  
  181.         sleep(RING_TIME);
  182.  
  183.         number=read(fd_modem, &buf, 32);
  184.     buf[number]='\0';
  185.  
  186.         if (strstr(buf, OK) == NULL)
  187.     {
  188.                 syslog(LOG_ERR,strerror(errno));
  189.                 shut_down(-1);
  190.     }
  191.  
  192.         if (strstr(buf, RING) != NULL )
  193.                 return (1);
  194.  
  195.     return (0);
  196. }
  197.  
  198. int get_rings(int rings_wanted) /* Waits for a number of specified rings. */
  199. {
  200.     int counter=0, done=0;
  201.     time_t time1=0, time2=0;
  202.     fd_set myset;
  203.  
  204.     FD_ZERO(&myset);
  205.  
  206.     while (!done)  /* BTW, read() is slightly broken, so i use select() */
  207.     {
  208.         syslog(LOG_NOTICE,"read() started");
  209.         FD_SET(fd_modem, &myset);
  210.         select(2,&myset,NULL,&myset,NULL);
  211.         counter = read(fd_modem, &buf, 256);
  212.         syslog(LOG_NOTICE,"read() ended, counter = %i",counter);
  213.         buf[counter]='\0';
  214.         syslog(LOG_NOTICE,"read() ended, buf = %s",buf);
  215.  
  216.         if ((counter > 0) && (strstr(buf, RING) != NULL))
  217.         {
  218.                     time(&time1);
  219.  
  220.             if ((time2 == 0) && ((abs(difftime(timer1, time1)) < TOTAL_TIME)))
  221.             {
  222.                 rings++;
  223.                 time2 = time1;
  224.             }
  225.             else
  226.             {
  227. /*                syslog(LOG_NOTICE,"time1=%ld,time2=%ld,diff=%f",time1,time2,difftime(time1,time2));
  228.                 syslog(LOG_NOTICE,"timer1=%ld,time2=%ld,diff=%f",timer1,time2,difftime(time2,timer1)); */
  229.                 if (((abs(difftime(time1, time2))) < RING_TIME) &&
  230.                     ((abs(difftime(timer1, time2))) < TOTAL_TIME))
  231.                 {
  232.                     rings++;
  233.                     time2 = time1;
  234.                 }
  235.                 else
  236.                     return(-1);
  237.             }
  238.             syslog(LOG_NOTICE,"rings = %i",rings);
  239.         }
  240.  
  241.         if (rings == rings_wanted)
  242.         {
  243.             syslog(LOG_NOTICE,"got rings_wanted, checking for more");
  244.             if (modem_reset() == 0)
  245.                 done=1;
  246.             else
  247.                 return(-1);
  248.         }
  249.     }
  250.  
  251.     return(0);
  252. }
  253.  
  254. void shut_down(int status)    /* Shut down somewhat gracefully. */
  255. {
  256.     if (status != SIGTERM)
  257.         syslog(LOG_NOTICE,"internal error occured, shutting down");
  258.     else
  259.         syslog(LOG_NOTICE,"SIGTERM recieved, shutting down");
  260.  
  261.     closelog();
  262.     flock(fd_modem,LOCK_UN);
  263.     close(fd_modem);
  264.     exit(status);
  265. }
  266.